/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts.dao;

//~--- non-JDK imports --------------------------------------------------------
import com.myapp.struts.actionform.SupplierMasterFormBean;
import com.myapp.struts.prepare.PrepareSuppler;

import org.apache.struts.action.ActionForm;

//~--- JDK imports ------------------------------------------------------------

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author pavan
 */
public class SupplierDao {

    private Connection con;
    private String deleteSupplier;
    private Connect drv;
    private SupplierMasterFormBean form1;
    private String insertSupplier;
    private PreparedStatement pstmt;
    private PrepareSuppler psupplier;
    private int status;
    private String supplierNameQuery;
    private ResultSet suppliers;
    private String updateSupplier;
    private Vector vec;

    public SupplierDao() {
        psupplier = new PrepareSuppler();
        vec = new Vector();
    }

    public boolean delete(SupplierMasterFormBean sbean) {
        try {
            con = getConnect();
            deleteSupplier = "delete from supp_master where suppname=?";
            pstmt = con.prepareStatement(deleteSupplier);
            pstmt.setString(1, sbean.getSupplierName());
            status = pstmt.executeUpdate();
            con.close();
            pstmt.close();

            if (status >= 1) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException ex) {
            Logger.getLogger(SupplierDao.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }

    public Connection getConnect() {
        drv = new Connect();
        con = drv.mySql();

        return con;
    }

    public boolean insert(ActionForm form) {
        try {

            /** Type cast ActionForm variable to FormBean */
            form1 = (SupplierMasterFormBean) form;

            /** Take Database Connection */
            con = getConnect();
            insertSupplier = "insert into supp_master values(?,?,?,?,?,?,?,?,?,?)";
            pstmt = con.prepareStatement(insertSupplier);
            pstmt.setString(1, form1.getSupplierName());
            pstmt.setString(2, form1.getAddress());
            pstmt.setString(3, form1.getPhoneNo());
            pstmt.setString(4, form1.getFaxNo());
            pstmt.setString(5, form1.getPanNo());
            pstmt.setString(6, form1.getStaxNum());
            pstmt.setString(7, form1.getStaxType());
            pstmt.setString(8, form1.getMonitoryLimit());
            pstmt.setString(9, form1.getTandc());
            pstmt.setString(10, form1.getLocation());
            status = pstmt.executeUpdate();
            con.close();
            pstmt.close();

            if (status >= 1) {
                return true;
            } else {
                return false;
            }
        } catch (Exception ex) {
            Logger.getLogger(SupplierDao.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }

    public Vector getSupplierNames() {
        try {
            con = getConnect();
            supplierNameQuery = "select distinct(suppname) from supp_master";
            pstmt = con.prepareStatement(supplierNameQuery);
            suppliers = pstmt.executeQuery();
            vec = psupplier.prepareSupplierName(suppliers);
            con.close();
            pstmt.close();
        } catch (Exception ex) {
            Logger.getLogger(LocationMasterDao.class.getName()).log(Level.SEVERE, null, ex);
        }

        return vec;
    }

    public Vector getRows() {
        try {
            con = getConnect();
            supplierNameQuery = "select * from supp_master";
            pstmt = con.prepareStatement(supplierNameQuery);
            suppliers = pstmt.executeQuery();
            vec = psupplier.prepareSupplier(suppliers);
            con.close();
            pstmt.close();
        } catch (Exception ex) {
            Logger.getLogger(LocationMasterDao.class.getName()).log(Level.SEVERE, null, ex);
        }

        return vec;
    }

    public boolean update(SupplierMasterFormBean sbean) {
        try {
            con = getConnect();
            updateSupplier =
                    "update supp_master set addr=?, phone=? ,fax=? , pan =?, staxno=? , saltaxtyp=?, mlmt=?, payterm=?, locationcode=?" + " where suppname=?";
            pstmt = con.prepareStatement(updateSupplier);
            pstmt.setString(10, sbean.getSupplierName());
            pstmt.setString(1, sbean.getAddress());
            pstmt.setString(2, sbean.getPhoneNo());
            pstmt.setString(3, sbean.getFaxNo());
            pstmt.setString(4, sbean.getPanNo());
            pstmt.setString(5, sbean.getStaxNum());
            pstmt.setString(6, sbean.getStaxType());
            pstmt.setString(7, sbean.getMonitoryLimit());
            pstmt.setString(8, sbean.getTandc());
            pstmt.setString(9, sbean.getLocation());
            status = pstmt.executeUpdate();
            con.close();
            pstmt.close();

            if (status >= 1) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException ex) {
            Logger.getLogger(SupplierDao.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;
    }
}
